package y2016.date0901.pm.二维数组闭环替换;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @author 问题来自
 * http://bbs.csdn.net/topics/391948572?page=1#post-401127140
 * 提问者：http://my.csdn.net/sozdream
 * 作者：http://my.csdn.net/piaopiao11
 */
public class Piaopiao {

    private static int nums[][]
            = {
            {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0},
            {0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1},
            {0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0},
            {0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0},
            {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
            {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0},
            {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0},
            {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0},
            {0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
            {1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
            {1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
            {1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};

    @Test
    public void main() throws Exception {
        init();
        b();
        c();
        print();
    }


    public static void init() {
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums[i].length; j++) {
                if (nums[i][j] == 0 && (i == 0 || i == nums.length - 1 || j == 0 || j == nums[i].length - 1)) {
                    nums[i][j] = 9;
                }
            }
        }
    }

    public static void b() {
        int flag = 0;
        for (int i = 1; i < nums.length - 1; i++) {
            for (int j = 1; j < nums[i].length - 1; j++) {
                if (ex(i, j)) flag = 1;
            }
            for (int j = nums[i].length - 2; j > 1; j--) {
                if (ex(i, j)) flag = 1;
            }
        }
        for (int i = nums.length - 2; i > 1; i--) {
            for (int j = 1; j < nums[i].length - 1; j++) {
                if (ex(i, j)) flag = 1;
            }
            for (int j = nums[i].length - 2; j > 1; j--) {
                if (ex(i, j)) flag = 1;
            }
        }
        if (flag == 1) {
//			print();
            System.out.println("re do--");
            b();
        }
    }

    private static boolean ex(int i, int j) {
        if (nums[i][j] == 0 && (nums[i][j - 1] == 9 || nums[i - 1][j] == 9 || nums[i][j + 1] == 9 || nums[i + 1][j] == 9)) {
            nums[i][j] = 9;
            return true;
        }
        return false;
    }


    public static void c() {
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums[i].length; j++) {
                if (nums[i][j] == 0) {
                    nums[i][j] = 2;
                }
                if (nums[i][j] == 9) {
                    nums[i][j] = 0;
                }
            }
        }
    }

    public static void print() {
        for (int[] num : nums) {
            System.out.println(Arrays.toString(num));
        }
    }


}


